// pages/changePhone/changePhone.js
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showoldPhone: true,
    phone: '',
    sixteenS: '发送验证码',
    sms: ''
  },
  updateFormerPhoneSendSMS(){
    var that = this
    if(!(/^1[3456789]\d{9}$/.test(this.data.phone))){ 
      Toast("手机号码有误，请重填");  
      return false; 
    } 
    if (this.data.sixteenS == '发送验证码') {
      wx.request({
        url: app.globalData.baseUrl + '/user/updateFormerPhoneSendSMS',
        data: {
          xcxOpenId: app.globalData.xcxOpenId
        },
        method:'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success:function(res){
          if (res.data.code == 0) {
            var s = 60
            that.setData({
              sixteenS: s + 's'
            })
            let time = setInterval(function(){
              that.setData({
                sixteenS: --s + 's'
              })
              if(s == 0){
                clearInterval(time)
                that.setData({
                  sixteenS: '发送验证码'
                })
              }
            },1000)
          }else {
            Toast(res.data.msg);
          }
        }
      })
    }
    
  },
  next(){
    wx.request({
      url: app.globalData.baseUrl + '/user/updateFormerPhoneVerifySMS',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
        code: this.data.sms
      },
      method:'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success:function(res){
        if (res.data.code == 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          wx.reLaunch({
            url: '../newphone/newphone',
          })
        }
      }
    })
  },
  changesms(e){
    this.data.sms = e.detail
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/user/getUserByOpenId',
      data: {
        xcxOpenId: app.globalData.xcxOpenId
      },
      success(res) {
        if (res.data.code == 0) {
          // console.log(res.data.data)
          if (!res.data.data.phone) {
            wx.navigateTo({
              url: '../newphone/newphone'
            })
          }else {
            that.setData({
              phone: res.data.data.phone
            })
          }
        }
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})